﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Presentation.ViewModels;

namespace Presentation.Views
{
    /// <summary>
    /// Interaction logic for AnecdotalListView.xaml
    /// </summary>
    public partial class AnecdotesView : UserControl
    {
        public AnecdotesView()
        {
            InitializeComponent();
        }

        private void AutoCompleteBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                AnecdotesViewModel anecdotesViewModel = (this.DataContext as AnecdotesViewModel);
                AutoCompleteBox acb = ((AutoCompleteBox)(sender));

                if (anecdotesViewModel.CanAddNewAnecdote)
                {
                    if (acb.IsDropDownOpen)
                    {
                        KeyEventArgs tabKeyDown = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, Key.Tab);
                        tabKeyDown.RoutedEvent = Keyboard.KeyDownEvent;

                        InputManager.Current.ProcessInput(tabKeyDown);
                    }
                    else
                    {
                        anecdotesViewModel.AddNewAnecdote();

                        List<KeyEventArgs> tabPresses = new List<KeyEventArgs>(6);

                        for (int i = 0; i < 8; i++)
                        {
                            KeyEventArgs tabKeyDown = new KeyEventArgs(Keyboard.PrimaryDevice, Keyboard.PrimaryDevice.ActiveSource, 0, Key.Tab);
                            tabKeyDown.RoutedEvent = Keyboard.KeyDownEvent;

                            tabPresses.Add(tabKeyDown);
                        }

                        foreach (KeyEventArgs keyEventArgs in tabPresses)
                        {
                            InputManager.Current.ProcessInput(keyEventArgs);
                        }
                    }
                }

            }
        }
    }
}
